Repository navigation
feat(core)!: async hooks, hook timeouts, input rewriting and subagent hooks - #144
Merged
Merged
Conversation
gopaljigaur
commented
Oct 15, 2026
gopaljigaur
left a comment
Owner
Author
There was a problem hiding this comment.
Every hook call site on main goes through _call_hook (all 15), and the order is unchanged. The conflict resolutions in run.py, fallback.py and compaction.py change only the hook lines. Subagent hooks pair with current_tool_call_id() for parallel calls, and cancellation behaves on 3.10. The new tests fail on main except the base hooks guard. Checks pass: pytest -n 4 (728), pre-commit, mypy, the site build, CI, and the branch is on main. Four findings:
- With
hook_timeoutset, plain hooks run in threads:TelemetryHooksthen records no spans, and plain hooks run concurrently. Applying the timeout to async hooks only removes both (inline onhooks.py). - A replacement that fails the input check skips
on_run_error(inline onrun.py). - Using
on_run_start's return value changes existing behaviour; the commit should be marked breaking (inline onhooks.py). - Missing tests: a timed out
on_tool_start, cancellation during a hook, and a cancelled subagent's end hook (inline ontests/test_hooks.py).
… hooks Every AgentHooks method may now be async def and is awaited. on_run_start may return text or content parts to replace the run's input, checked as run input is. on_subagent_start and on_subagent_end fire on the calling agent's hooks around each nested subagent run. AgentConfig.hook_timeout caps each hook call; a hook past it raises TimeoutError, as a hook that raises does. BREAKING CHANGE: the return value of AgentHooks.on_run_start is no longer ignored. Any value other than None replaces the run's input, so a hook (or a mock standing in for AgentHooks) that returns something must return None to keep the input.
hook_timeout no longer moves plain hooks to a worker thread, which lost the context variables they set and ran them concurrently; plain hooks run inline and only async hooks are bounded. on_run_start and the check of its replacement now run inside the run's error handling, so on_run_error fires when either fails.
gopaljigaur
force-pushed
the
feat/more-hooks
branch
from
October 15, 2026 08:26
75c712f to
e2bebc7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds the hook points and timeouts from #72 that were still missing, in the shapes the hooks and middleware already use.
What changed
AgentHooksmethod may beasync def; it is awaited. Detection isinspect.iscoroutinefunction, as Pydantic AI does for history processors. The base methods are typedMaybeAwaitable[...]so async overrides type-check.on_run_startmay return text or content parts to replace the run's input, the waybefore_toolreturns new arguments (and LangChain'sbefore_agentreturns a state update).Nonekeeps the input. The replacement goes through the same check as run input, and it is what the model and the session get. No new hook name. Its return value used to be ignored, so a hook or mock that returns something must now returnNone. The hook and the check run inside the run's error handling, soon_run_errorfires when either fails.on_subagent_start(agent_path, input_message)andon_subagent_end(agent_path, result)fire on the calling agent's hooks around each nestedAgentToolrun;on_subagent_endgets theRunResulthowever the run ended. Only the direct caller's hooks get them, so a shared hooks object is not called twice for deeper nesting. Named after the existing subagent terms (max_subagent_depth,cyclops.core.subagents); the OpenAI Agents SDK's closest pair ison_agent_start/on_agent_end.AgentConfig.hook_timeout. Seconds each async hook call may run, besidetool_timeout, usingasyncio_timeoutfromcyclops/utils/compat.py. A hook past it is cancelled and raisesTimeoutError, and the run treats it as a hook that raised. This keepson_tool_startfail closed (a slow approval never allows a call) and needs no option. Plain hooks run inline as before and are not bounded, since asyncio cannot interrupt blocking code; the guide says to write a hook asasync defto bound it._call_hook.docs/and on the website.Issue items that already existed
on_compaction(summary, tokens_before, tokens_after)came with feat: summarize older messages near the model's input limit #118, and the summarizing completion already fireson_llm_start/on_llm_end. Nothing added.AgentToolruns already sent their events to the outermost run withagent_path, and the caller's hooks saw them as an ordinary tool call (on_tool_start/on_tool_end). No hook saw the nested run itself.ToolMiddlewareand guards were already async;AgentHookswas sync only.AgentHooksand timeouts did not exist.Tests
tests/test_hooks.py: replacing the input (text, and context added as parts from an async hook); a replacement that fails the input check and a timed outon_run_start, both firingon_run_error; async hooks awaited in order; timeouts on async hooks, including a timed outon_tool_startwhose tool never runs; plain hooks running inline underhook_timeout; hooks within their timeout; cancelling a run during a hook; base hooks sending the same requests as no hooks; and subagent runs (completed, failed, cancelled, and nested two deep) seen by the caller's hooks. MockedAgentHooksin existing tests now seton_run_start.return_value = None, since a mock's return value would otherwise replace the input.uv run pytest -n 4(732 passed, also on 3.10),uv run pre-commit run --all-filesanduv run mypy cyclopspass.Closes #72